<?php
defined('BASEPATH') OR exit('No direct script access allowed');


use QCloud_WeApp_SDK\Mysql\Mysql as DB;


class OrderAdd extends CI_Controller
{
    public function index()
    {
        $userid = $_POST['userid'];
        $amount = $_POST['amount'];
        $name = $_POST['name'];
        $mobile = $_POST['mobile'];
        $sheng = $_POST['sheng'];
        $shi = $_POST['shi'];
        $qu = $_POST['qu'];
        $address = $_POST['address'];
        $zt = $_POST['zt'];
        $zffs = $_POST['zffs'];
        $remark = $_POST['remark'];
        $goods = $_POST['goods'];
        $content = $_POST['content'];
        $has_coupon = $_POST['has_coupon'];
        $discount = $_POST['discount'];


        $rand = rand(0, 10000);
        $insert_time = date("Y-m-d H:i:s", intval(time()));
        $ordernum = 'M' . '' . date("YmdHis", intval(time())) . '' . $rand;
        $goodsnum = 0;
        $de_json = json_decode($goods, TRUE);
        for ($i = 0; $i < count($de_json); $i++) {
            $good = $de_json[$i];
            $pic = $good["pic"];
            $goodsnum += $good["num"];
            $rows = DB::insert('cc_order_detail', ['ordernum' => $ordernum, 'pic' => $pic, 'num' => $good["num"],
                'goods_id' => $good["goods_id"], 'goods_name' => $good["goods_name"], 'price' => $good["price"],'pic' => $good["pic"],'add_time' => $insert_time]);
        }
        $goods_sku = '';
        if (count($de_json) > 1) {
            $goods_sku = $de_json[0]['goods_name'] . '...等' . count($de_json) . '件商品';
        } else {
            $goods_sku = $de_json[0]['goods_name'];
        }
        $pic = $_POST['pic'];
        $price = $_POST['price'];
        $rows = DB::insert('cc_order', [
            'userid' => $userid,
            'amount' => $amount,
            'ordernum' => $ordernum,
            'name' => $name,
            'mobile' => $mobile,
            'sheng' => $sheng,
            'shi' => $shi,
            'qu' => $qu,
            'address' => $address,
            'remark' => $remark,
            'mobile' => $mobile,
            'zt' => $zt,
            'zffs' => $zffs,
            'add_time' => $insert_time,
            'num' => $goodsnum,
            'insert_time' => $insert_time,
            'goods_sku' => $goods_sku,
            'add_time' => $insert_time,
            'price' => $price,
            'pic' => $pic,
            'has_coupon' => $has_coupon,
            'discount' => $discount,

        ]);
        if (!empty($rows)) {
            $rows = $ordernum;
        }

        if (!empty($rows)) {
            $this->json([
                'code' => 0,
                'data' => $rows
            ]);
        } else {
            $this->json([
                'code' => -1,
                'error' => $rows['error']
            ]);
        }
    }
}
